<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 


class Article_mdl extends MY_Model {

	const DB_ARTICLE = 'article';		//食材数据库
	const DB_CATEGORY = 'category';		//食材分类数据库

    public function __construct()
    {
		parent::__construct();
		$this->table = 'article';
    }
    //获取文章内容
    public function get_article_by_id($id)
    {
    	$this->load->library('category');
    	$data = array();
		$this->db->select('*')->from(self::DB_ARTICLE)->where('id', intval($id))->limit(1);
		$query = $this->db->get();
		if($query->num_rows() == 1)
		{
			$data = $query->row_array();
			$cate = $this->category->fetchOne($data['cid']);
			$data['addtime'] = date("Y-m-d h:i", $data['addtime']);
			@$data['cateName'] = $cate['cateName'];
		}
		$query->free_result();
		
		return $data;
    }

    //获取文章列表classid
    public function get_article_list($cid = NULL,$offset = NULL, $limit = NULL, $orderby = 'id DESC')
    {
		$this->db->select('*');
		if($cid && is_numeric($cid))
		{
			$this->db->where('cid', intval($cid));
		}
		if($limit && is_numeric($limit))
		{
			$this->db->limit(intval($limit));
		}
		
		if($offset && is_numeric($offset))
		{
			$this->db->offset(intval($offset));
		}
		$this->db->order_by($orderby); 
        return $this->db->get(self::DB_ARTICLE);
    }


    //获取文章分类 0 所有，pid获取大分类下子分类
    public function get_article_category($fatherId = 0,$offset = NULL, $limit = NULL)
    {
		$this->db->select('cid,cateName');
		$this->db->where('fatherId', intval($fatherId));

		if($limit && is_numeric($limit))
		{
			$this->db->limit(intval($limit));
		}
		
		if($offset && is_numeric($offset))
		{
			$this->db->offset(intval($offset));
		}

        return $this->db->get(self::DB_CATEGORY);
    }

    //搜索
    public function get_article_search($keywords, $offset = NULL, $limit = NULL, $orderby = 'id DESC')
    {
    	$keywords = urldecode($keywords);
    	$keywords = addslashes($keywords);
		$this->db->select('*');
		$this->db->like('title', $this->db->escape_like_str($keywords));

		if($limit && is_numeric($limit))
		{
			$this->db->limit(intval($limit));
		}
		
		if($offset && is_numeric($offset))
		{
			$this->db->offset(intval($offset));
		}

		$this->db->order_by($orderby); 

        return $this->db->get(self::DB_ARTICLE);
    }

    public function update_views($id)
    {
		$this->db->set('views','views+1',FALSE);
		$this->db->where('id',$id);
    	return $this->db->update(self::DB_ARTICLE);
    }

	//***********************后台专用*********************************************************************
	public function article_insert($arr)
	{
		return $this->db->insert(self::DB_ARTICLE,$arr);
	}

	public function article_update($arr, $id)
	{
		return $this->db->update(self::DB_ARTICLE, $arr, 'id = '.$id);
	}

	public function article_delete($id)
	{
		$this->load->library('alioss');
		$ser_img = 'http://img.ichufang.cn/';
		$this->db->select('pic,content')->where('id', intval($id))->limit(1);
		$query = $this->db->get(self::DB_ARTICLE)->row_array();
		//删除缩略图
		if ($query['pic'])
		{
			$img_path = str_replace($ser_img, '', $query['pic']);
			@unlink('./uploads/'.$img_path);
			$this->alioss->delete_object('ichufang',$img_path);
		}

		//删除内容中 图片
		$pattern="/<img.+?src=[\\\\]?\"(.+?)[\\\\]?\"/i";
		preg_match_all($pattern,$query['content'], $img_array, PREG_PATTERN_ORDER);
		$img_array = array_unique($img_array[1]);
		if ($img_array)
		{
			foreach ($img_array as $img) 
			{
				$img_path = str_replace($ser_img, '', $img);
				@unlink('./uploads/'.$img_path);
				$this->alioss->delete_object('ichufang',$img_path);

			}
		}
		//删除内容
		/*阿里云*/
		//$objects = array('myfoloder-1349850940/','myfoloder-1349850941/',);   
		//$this->alioss->delete_objects('ichufang',$objects,array('quiet' => false));
		/*阿里云结束*/

		return $this->db->where('id',$id)->delete(self::DB_ARTICLE);
	}
}

/* End of file shicai_model.php */
/* Location: ./application/models/shicai_model.php */